package com.woniuxy.huayoung.controller;

import com.woniuxy.huayoung.entity.*;
import com.woniuxy.huayoung.result.ResponseResult;
import com.woniuxy.huayoung.service.*;
import com.woniuxy.huayoung.utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;
    @Resource
    private OrderService orderService;
    @Resource
    private HotelService hotelService;
    @Resource
    private TravelGoodsService travelGoodsService;
    @Resource
    private FlightInfoService flightInfoService;


    @GetMapping("/notpay")
    public ResponseResult<List<Order>> notPay(HttpServletRequest request){
        //得到token
        String token = request.getHeader("authorization");
        int uid = userService.findIdByToken(token);
        //
        List<Order> orders = orderService.notPay(uid);
        return new ResponseResult<List<Order>>(200,"查询成功",orders);
    }

    @GetMapping("/havepay")
    public ResponseResult<List<Order>> havePay(HttpServletRequest request){
        //得到token
        String token = request.getHeader("authorization");
        int uid = userService.findIdByToken(token);
        //
        List<Order> orders = orderService.havePay(uid);
        return new ResponseResult<List<Order>>(200,"查询成功",orders);
    }

    @GetMapping("/detail")
    public ResponseResult<Object> detail(@RequestParam("goodsid") Integer goodsid, @RequestParam("type") Integer type){
        logger.debug(goodsid+"-------"+type);
        if(type == 1){
            //查旅游商品
            TravelGoods travelGoods = travelGoodsService.findByid(goodsid);
            return new ResponseResult<Object>(200,"旅游商品",travelGoods);
        }
        else if(type == 2){
            //查酒店
            Hotel hotel = hotelService.findById(goodsid);
            return new ResponseResult<Object>(200,"酒店",hotel);
        }
        else{
            //查机票
            FlightInfos flightInfos = flightInfoService.findByid(goodsid);
            return new ResponseResult<Object>(200,"机票",flightInfos);

        }
        //return new ResponseResult<Object>(200,"fail",null);
    }

    @PostMapping("/pay")
    public ResponseResult<Object> Pay(@RequestBody Map<String,String> map){
        String orderno = map.get("orderno");
        int pay = orderService.pay(orderno);
        if(pay == 1){
            return new ResponseResult<Object>(200,"支付成功",null);
        }else if(pay == 0){
            return new ResponseResult<Object>(500,"支付失败",null);
        }else{
            return new ResponseResult<Object>(500,"服务器异常",null);
        }


    }
    //查找所有酒店订单
    @GetMapping("/all")
    public ResponseResult<List<Order>> all() {

        List<Order> list = orderService.all();


        //将数组进行封装返回
        ResponseResult<List<Order>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(list);
        //
        System.out.println(list);
        return responseResult;
    }
    //查找所有机票订单
    @GetMapping("/ticket")
    public ResponseResult<List<Order>> ticket() {

        List<Order> list = orderService.ticket();


        //将数组进行封装返回
        ResponseResult<List<Order>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(list);
        //
        System.out.println(list);
        return responseResult;
    }
    //查找所有旅游订单
    @GetMapping("/travel")
    public ResponseResult<List<Order>> travel() {

        List<Order> list = orderService.travel();


        //将数组进行封装返回
        ResponseResult<List<Order>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(list);
        //
        System.out.println(list);
        return responseResult;
    }
    //根据订单编号查找订单
    @GetMapping("/findByOrderno")
    public ResponseResult<Order> findByOrderno(String orderNo) {

        Order order = orderService.findOrder(orderNo);


        //将数组进行封装返回
        ResponseResult<Order> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(order);
        //

        return responseResult;
    }
}
